package com.lt.accesscontrolmanager.controller;

import com.lt.accesscontrolmanager.config.OssService;
import com.lt.accesscontrolmanager.service.AccessControlDeviceSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/images")
@CrossOrigin(origins = "http://localhost:5173/")
public class ImageController {

    @Autowired
    private OssService ossService;

    @Autowired
    private AccessControlDeviceSerivce accessControlDeviceSerivce;

    @PostMapping("/upload")
    public ResponseEntity<String> uploadImage(@RequestParam("file") MultipartFile file,@RequestParam("id") String id) {
        System.out.println(id+"aaasasa");
        String imageUrl = ossService.uploadFile(file);

        System.out.println(imageUrl+"ddd");
        accessControlDeviceSerivce.updateImg(id,imageUrl);
        return ResponseEntity.ok(imageUrl);
    }

    @DeleteMapping("/delete")
    public ResponseEntity<String> deleteImage(@RequestParam String fileUrl) {
        ossService.deleteFile(fileUrl);
        return ResponseEntity.ok("Image deleted successfully.");
    }
}
